﻿using AutoMapper;
using BasicData.Application.Input;
using BasicData.Application.Input.Supplier;
using BasicData.Application.Output;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using BasicData.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BasicData.Application.Services.Supplier
{
    public class SupplierServices : ISupplierServices
    {
      IBasicModelRepository<SupplierModel> _repository;
        IMapper mapper;

        public SupplierServices(IBasicModelRepository<SupplierModel> repository, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
        }

        public ApiResult<int> AddSupplier(SupplierInput input)
        {
            var supplier = mapper.Map<SupplierModel>(input);
            supplier.Id = YitIdHelper.NextId();
            supplier.CreatedAt = DateTime.Now;
            supplier.CreatedBy = "wyj";
            _repository.Create(supplier);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "添加成功",
                Data = 1
            };
        }

        public ApiResult<int> DeleteSupplier(long id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _repository.GetValue().FirstOrDefault(x => x.Id == id);
            if (model == null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "未找到该数据";
                return res;
            }
            model.IsDeleted = true;
            int i = _repository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Error;
                res.Message = "删除失败";
                res.Data = i;
            }
            return res;
        }

        public ApiPaging<SupplierOutput> GetSupplier(PageInput input)
        {
            ApiPaging<SupplierOutput> res = new ApiPaging<SupplierOutput>();
            var supplier = _repository.GetValue().Where(x => x.IsDeleted == false);
            var query = supplier.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var list = mapper.Map<List<SupplierOutput>>(query);
            return new ApiPaging<SupplierOutput>()
            {
                PageDatas = list,
                TotalCount = supplier.Count(),
                Code = ApiEnums.OK,
                Message = "获取成功",

            };
        }

        public ApiResult<int> UpdateSupplier(UpdateSupplierInput input)
        {
            var modedl = mapper.Map<SupplierModel>(input);
            modedl.UpdatedAt = DateTime.Now;
            modedl.CreatedBy = "wyj";
            _repository.Update(modedl);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "修改成功",

            };
        }
    }
}
